/*
 * Copyright 2011 Andy Lee.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package uk.co.optimisticpanda.remoteweb.command.web

import org.testng.Assert;

import uk.co.optimisticpanda.remoteweb.command.Executable
import uk.co.optimisticpanda.remoteweb.command.Variable;

class WebExecutable extends Executable{

	Map args
	WebActionType type

	@Override
	public String toString() {
		return args.toString();
	}

	public void call(arg){
		getClosure()(arg)
	}

	private def getClosure(){
		def callArgs = [variableFinder: this.variableFinder]
		callArgs.putAll(args);
		type.getClosure(callArgs)
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((args == null) ? 0 : args.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this.is(obj)){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		WebExecutable other = (WebExecutable) obj;
		if (args == null) {
			if (other.args != null){
				return false;
			}
		} else if (args != other.args){
			return false;
		}
		if (type != other.type){
			return false;
		}
		return true;
	}
}